package com.buyingtickets.controller;

import com.buyingtickets.dao.AdminSiteDao;
import com.buyingtickets.entity.QueryParameter;
import com.buyingtickets.entity.Site;
import com.buyingtickets.entity.UnitInterval;
import com.buyingtickets.service.SiteService;
import com.buyingtickets.service.UnitIntervalBiz;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author czl
 * @version 1.0
 * @description: TODO
 * @date 2021/6/25 10:03
 */
@Controller
@RequestMapping("unit")
public class UnitController {
    @Resource
    private UnitIntervalBiz unitIntervalBiz;
@Resource
private SiteService siteService;
    @RequestMapping("query")
    public String unit(QueryParameter qp,Model model){
        System.out.println(qp);
        List<UnitInterval> unitList=unitIntervalBiz.queryByQueryUnitInterval(qp);
        for(UnitInterval u:unitList){
            System.out.println(u);
        }
         model.addAttribute("unitList",unitList);
         model.addAttribute("qp",qp);
        return "admin_unitinterval_query";
    }

    @RequestMapping("{page}/{unitID}/{qishi}/{zhongdian}/{lucheng}/{shijian}/fenye")
    public String fenye(@PathVariable int page,@PathVariable String unitID, @PathVariable String qishi, @PathVariable String zhongdian, @PathVariable String lucheng,@PathVariable String shijian, Model model){
        System.out.println(page+" "+unitID+" "+qishi+" "+zhongdian+" "+lucheng+" "+shijian);
        QueryParameter qp=new QueryParameter();
        if (!"!".equals(page)) {
            qp.setPage(page);
        }
        if (!"!".equals(unitID)) {
            qp.setUnitID(unitID);
        }
        if (!"!".equals(qishi)) {
            qp.setQishi(qishi);
        }
        if (!"!".equals(zhongdian)) {
            qp.setZhandian(zhongdian);
        }
        if (!"!".equals(lucheng)) {
            qp.setLucheng(lucheng);
        }
        if (!"!".equals(shijian)) {
            qp.setShijian(shijian);
        }

        return unit(qp,model);
    }
    @RequestMapping("unitinterval_details/{id}")
    public String site_details(@PathVariable int id,Model model){
        UnitInterval unit=unitIntervalBiz.queryByID(id);
        List<Site> site=siteService.querySiteAll();
        model.addAttribute("unit",unit);
        model.addAttribute("site",site);
        return "admin_unitinterval_details";
    }


    @RequestMapping(value = "update",method = RequestMethod.POST)
    @ResponseBody
    public int update(UnitInterval unit){
        System.out.println(unit);
        return unitIntervalBiz.update(unit);
    }

    @RequestMapping( "delete")
    @ResponseBody
    public int delete(int id){
      return unitIntervalBiz.delete(id);
    }
    @RequestMapping("unit_add")
    public String add(Model model){
        List<Site> site=siteService.querySiteAll();
        model.addAttribute("site",site);
        return "admin_unitinterval_add";
    }
    @RequestMapping(value = "add",method = RequestMethod.POST)
    @ResponseBody
    public int add(UnitInterval unit){
        System.out.println(unit);
        return unitIntervalBiz.add(unit);
    }
}
